package org.bigjava.novelplatform.service.impl;

import cn.hutool.core.util.RandomUtil;
import lombok.SneakyThrows;
import org.bigjava.novelplatform.common.exception.ExceptionCodeMessage;
import org.bigjava.novelplatform.common.exception.UserActionException;
import org.bigjava.novelplatform.common.utils.EncryptUtils;
import org.bigjava.novelplatform.entity.Users;
import org.bigjava.novelplatform.mapper.ChapterSaysMapper;
import org.bigjava.novelplatform.mapper.UsersMapper;
import org.bigjava.novelplatform.service.admin.AdminServiceForBook;
import org.bigjava.novelplatform.service.admin.AdminServiceForUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class AdminServiceImpl implements AdminServiceForUser, AdminServiceForBook {
    @Resource
    UsersMapper usersMapper;
    @Resource
    ChapterSaysMapper chapterSaysMapper;

    static boolean MailboxFormatCheck(String owner) {//邮箱格式判断
        if (null == owner) {
            throw new UserActionException(ExceptionCodeMessage.NON_COMPLIANT_PARAMETERS);
        }
        String check = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        Pattern regex = Pattern.compile(check);
        //是否匹配
        Matcher matcher = regex.matcher(owner);
        return matcher.matches();
    }

    /**
     * 后台添加用户
     */
    @Override
    public boolean addUser(Users users) {
        if (!MailboxFormatCheck(users.getEmail())) {
            throw new UserActionException(ExceptionCodeMessage.UNSUPPORTED_MAIL);//判断是否为邮箱格式
        }
        String eamil = usersMapper.selectEmail(users.getEmail());
        if (null != eamil) {
            throw new UserActionException(ExceptionCodeMessage.USED_EMAIL);//该邮箱已经存在
        }
        if (null == users.getNickName() & null == users.getPassword() & null == users.getUserType()) {
            throw new UserActionException(ExceptionCodeMessage.NON_COMPLIANT_PARAMETERS);
        }
        String randomString = RandomUtil.randomString(32);
        users.setPassword(EncryptUtils.salt(users.getPassword(), randomString));
        users.setSecretKey(randomString);
        return usersMapper.insert(users) == 1;
    }

    /**
     * 后台搜索用户
     */
    @Override
    public List<Users> searchUser(String input, String searchRules) {
        if (null == searchRules) {
            throw new UserActionException(ExceptionCodeMessage.NON_COMPLIANT_PARAMETERS);
        }
        List<Users> list = new ArrayList<>();
        if ("邮箱".equals(searchRules)) {
            if (!MailboxFormatCheck(input)) {
                throw new UserActionException(ExceptionCodeMessage.UNSUPPORTED_MAIL);
            }
            Users users1 = usersMapper.selectUserByEmail(input);
            if (null != users1) {
                list.add(users1);
            }
        } else {
            list = usersMapper.SearchNickname(input);
        }
        return list;
    }

    /**
     * 后台修改用户信息
     */
    @Override
    public boolean updateUser(Users users) {

        return false;
    }

    /**
     * 根据用户email删除用户
     */
    @Override
    public boolean deleteUser(String email) {
        if (!MailboxFormatCheck(email)) {
            throw new UserActionException(ExceptionCodeMessage.UNSUPPORTED_MAIL);
        }
        return usersMapper.updateDeleteStateByEmail(email) == 1;
    }

    /**
     * 根据用户email恢复已删除的用户
     */
    @Override
    public boolean recoveryUser(String email) {
        if (!MailboxFormatCheck(email)) {
            throw new UserActionException(ExceptionCodeMessage.UNSUPPORTED_MAIL);
        }
        return usersMapper.recoveryDeleteStateByEmail(email) == 1;
    }

    /**
     * 获取指定类型的用户数据
     */
    @Override
    public List<Users> getUserForType(Integer type) {

        return null;
    }

    /**
     * 分级分类获取用户数据
     */
    @SneakyThrows
    @Override
    public List<Users> getUserForType(String type1, String type2) {
        Integer type10;
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        switch (type1) {
//            case "普通用户":
//                type10 = 0;
//                if (null == type2) return usersMapper.getUserForType(type10);
//                switch (type2) {
//                    case "已禁言用户":
//                        return usersMapper.getImprisonUser(type10,0);
//                    case "老用户":
//                        return usersMapper.getOldUser(type10,100L);
//                    case "新用户":
//                        return usersMapper.getNewUser(type10, );
//                    default:
//                        return usersMapper.getUserForType(type10);
//                }
//                break;
            case "管理员":
                type10 = 1;
                if (null == type2) {
                    return usersMapper.getUserForType(type10);
                }
                switch (type2) {
                    case "已禁言用户":
                        return usersMapper.getImprisonUser(type10, 1);
                    case "老用户":
                        return usersMapper.getOldUser(type10, 100L);
                    case "新用户":
                        return usersMapper.getNewUser(type10, dateFormat1.parse("2022-05-01"));
                    default:
                        return usersMapper.getUserForType(type10);
                }
            case "作者":
                type10 = 3;
                if (null == type2) {
                    return usersMapper.getUserForType(type10);
                }
                switch (type2) {
                    case "已禁言用户":
                        return usersMapper.getImprisonUser(type10, 1);
                    case "老用户":
                        return usersMapper.getOldUser(type10, 100L);
                    case "新用户":
                        return usersMapper.getNewUser(type10, dateFormat1.parse("2022-05-01"));
                    default:
                        return usersMapper.getUserForType(type10);
                }
            case "编辑":
                type10 = 2;
                if (null == type2) {
                    return usersMapper.getUserForType(type10);
                }
                switch (type2) {
                    case "已禁言用户":
                        return usersMapper.getImprisonUser(type10, 1);
                    case "老用户":
                        return usersMapper.getOldUser(type10, 100L);
                    case "新用户":
                        return usersMapper.getNewUser(type10, dateFormat1.parse("2022-05-01"));
                    default:
                        return usersMapper.getUserForType(type10);
                }
            case "已注销":
                return usersMapper.getDeletedUser();
            default:
                type10 = 0;
                if (null == type2) {
                    return usersMapper.getUserForType(type10);
                }
                switch (type2) {
                    case "已禁言用户":
                        return usersMapper.getImprisonUser(type10, 1);
                    case "老用户":
                        return usersMapper.getOldUser(type10, 100L);
                    case "新用户":
                        return usersMapper.getNewUser(type10, dateFormat1.parse("2022-05-01"));
                    default:
                        return usersMapper.getUserForType(type10);
                }
        }
    }

    /**
     * 按指定的点踩数区间(长度为size)获取本章说 列：点踩数处于0-100之间的本章说有n章
     */
    @Override
    public List<Integer> getDownholeCount(Integer size) {
        ArrayList<Integer> list = new ArrayList<>();
        Integer head = 0;
        Integer tail = size;
        Integer max = chapterSaysMapper.selectDownholeCountMax();
        if (null == max) {
            return list;
        }
        Integer page = max / tail;
        if (tail * page != max) {
            page = (max / tail) + 1;
        }
        for (Integer i = 0; i < page; i++) {
            if (tail >= max) {
                tail = max;
            }
            list.add(chapterSaysMapper.selectChapterSaysPage(head, tail));
            head = tail;
            tail += size;
        }
        return list;
    }

    /**
     * 删除点踩数超过指定阀值的本章说
     */
    @Override
    public Boolean deleteDownholeCount(Integer size) {
        if (null == size) {
            return false;
        }
        return chapterSaysMapper.updateByDownholeCount(size) == 1;
    }

    /**获取用户统计页面的初始化数据*/
    @SneakyThrows
    public Map<String,Integer> getInitializationData(){
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Map<String,Integer> maps = new HashMap<>();
        maps.put("LoggedOff",usersMapper.getDeletedUserBynumber());//已注销
        maps.put("OrdinaryUsers",usersMapper.getTypeUserBynumber(0));//普通用户
        maps.put("Administrators",usersMapper.getTypeUserBynumber(1));//管理员
        maps.put("Edit",usersMapper.getTypeUserBynumber(2));//编辑
        maps.put("Author",usersMapper.getTypeUserBynumber(3));//作者
        maps.put("ForbiddenUsers",usersMapper.getImprisonUserBynumber());//已禁言
        maps.put("NewUser",usersMapper.getNewUserBynumber(dateFormat1.parse("2022-05-01")));//新用户
        maps.put("Oldusers",usersMapper.getOldUserBynumber(100L));//老用户
        return maps;
    }
}
